/*
 *
 *  *  Copyright © 2014 CIB Co.Ltd
 *  *  * All right reserved
 *  *  * Auther:jin xingwang
 *  *
 *
 */

package com.wsd.common;

import com.jwts.common.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 功能
 * @author 靳兴旺
 * @version 1.0.0 2020-7-16
 * Token工具类
 */
@Component
public class TokenUtils {


    @Value("${jwt.valid.time}")
    private int jwtValidTime;

    /**
     * 通过RSA生成系统默认值，使用系统默认密钥
     * @param notesId 管理员
     * @return token
     */
    public String createDefaultTokenStringRsa(String notesId) {
        String jwtToken = null;
        try {
            JwtClaims jwtClaims = new JwtClaims(notesId, FailureTimeUtils.createValidTime(FailureTimeEnum.DAY, jwtValidTime));
            jwtToken = Jwts.compact(Header.RSA, jwtClaims);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jwtToken.replaceAll("\r\n", "");
    }

    public String createDefaultTokenStringRsa(String openId, String customerId) {
        if ( StringUtils.isEmpty(openId) || StringUtils.isEmpty(customerId)) {
            throw new RuntimeException("入参不能为null");
        }
        String jwtToken = null;
        try {
            JwtClaims jwtClaims = new JwtClaims(openId, customerId, LocalDateTime.now());
            jwtToken = Jwts.compact(Header.RSA, jwtClaims);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jwtToken;
    }

}
